Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[core] Pre filter sources when querying by layer #6514

Merged
merged 4 commits into from
Oct 5, 2016

Conversation

tmpsantos
Copy link
Contributor

@tmpsantos tmpsantos commented Sep 29, 2016

Feature querying is extremelly slow because it goes through all the sources and these sources can be dense. This PR makes it possible to filter by source.

Annotations will benefit from it because they live in their own "virtual" source. Also if you have a low density source for say, road disruptions, you can make the query extremely responsive by querying only on this source.

@mention-bot
Copy link

@tmpsantos, thanks for your PR! By analyzing this pull request, we identified @jfirebaugh, @1ec5 and @incanus to be potential reviewers.

@tmpsantos
Copy link
Contributor Author

/cc @brunoabinader

@@ -199,6 +199,10 @@ std::unordered_map<std::string, std::vector<Feature>> Source::Impl::queryRendere
return result;
}

if (parameters.sourceIDs && parameters.sourceIDs->find(id) == parameters.sourceIDs->end()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer this check to be done in Style::queryRenderedFeatures - this way we can avoid the creation of result for sources not present in sourceIDs.

@tmpsantos
Copy link
Contributor Author

/cc @zugaldia @incanus think this was affecting Android and iOS at some point, right?

@zugaldia
Copy link
Member

@tmpsantos This is great. cc: @ivovandongen for adapting the Android API.

@tmpsantos
Copy link
Contributor Author

For the record Annotations will get a perf bump for free.

Copy link
Contributor

@jfirebaugh jfirebaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding new API surface area, can we optimize this case similarly to mapbox/mapbox-gl-js#2877?

@tmpsantos
Copy link
Contributor Author

tmpsantos commented Sep 29, 2016

Rather than adding new API surface area, can we optimize this case similarly to mapbox/mapbox-gl-js#2877

Not sure about pre-filtering automatically. Seems like a valid optimization when we don't set the source filter, but I like the filter parameter specially for annotations and the use case that motivated me write this PR in the first place: query features on from a source I created using the dataset editor. I wanted these features to get in sync with native views, so they needed to be queried extremely fast.

Looking at the code, the layers are stored in a std::vector and we would have to move it to a std::set to get reasonable performance on pre-filtering. Is there a reason for having it in a std::vector in the first place? Can we do both? i.e pre-filter if filter is not explicit.

@1ec5
Copy link
Contributor

1ec5 commented Sep 29, 2016

query features on from a source I created using the dataset editor

Wouldn’t #5792 be a more direct API for this use case?

@tmpsantos
Copy link
Contributor Author

Wouldn’t #5792 be a more direct API for this use case?

The low level core API I'm proposing could be used by the platform bindings for implementing this API.

@ivovandongen
Copy link
Contributor

Rather than adding new API surface area, can we optimize this case similarly to mapbox/mapbox-gl-js#2877

Isn't this complimentary to explicitly specifying the sources to query? I wouldn't mind a little more api surface to have that flexibility here.

@1ec5
Copy link
Contributor

1ec5 commented Sep 30, 2016

Wouldn’t #5792 be a more direct API for this use case?

The low level core API I'm proposing could be used by the platform bindings for implementing this API.

This would be a departure from how GL JS implements feature querying: querySourceFeatures() has quite a few nuances that make it good for the annotation querying use case but not a great candidate for folding into queryRenderedFeatures().

@tmpsantos tmpsantos force-pushed the tmpsantos-query_source_filter branch from 34dc6de to b178987 Compare October 4, 2016 11:56
@tmpsantos tmpsantos force-pushed the tmpsantos-query_source_filter branch from b178987 to 5e15919 Compare October 4, 2016 17:35
@tmpsantos tmpsantos changed the title [core] allow filter queries by source [core] Pre filter sources when querying by layer Oct 4, 2016
@tmpsantos
Copy link
Contributor Author

tmpsantos commented Oct 4, 2016

@jfirebaugh I re-enabled the benchmarks tests on this PR and wrote a benchmark for querying features. Pre-filtering sources gives a 64x performance bump when the layer you are looking for is in a source not so dense (like annotations or a geojson source).

Explicitly specifying the source makes no measurable difference compared to the pre-filter approach and exposes a new API surface. So let's do how you suggested. 👍

@tmpsantos
Copy link
Contributor Author

tmpsantos commented Oct 4, 2016

Pre-filtering sources:

Benchmark                              Time(ns)    CPU(ns) Iterations
---------------------------------------------------------------------
API_queryRenderedFeatures              14167512   14160000         50                                 
API_queryRenderedFeaturesLayerFilter      21569      21563      33019                                 

Before the patch:

Benchmark                              Time(ns)    CPU(ns) Iterations
---------------------------------------------------------------------
API_queryRenderedFeatures              14428871   14400000         50                                 
API_queryRenderedFeaturesLayerFilter    1309461    1304854        515 

Copy link
Contributor

@jfirebaugh jfirebaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@tmpsantos tmpsantos force-pushed the tmpsantos-query_source_filter branch from 5e15919 to bd21249 Compare October 4, 2016 21:11
@tmpsantos tmpsantos merged commit a38104e into master Oct 5, 2016
@tmpsantos tmpsantos deleted the tmpsantos-query_source_filter branch October 5, 2016 08:12
@1ec5 1ec5 mentioned this pull request Oct 17, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants